package gov.dhs.cis.soa.readers;

import gov.dhs.cis.soa.SOAData;
import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.data.IntegrationComp;
import gov.dhs.cis.soa.data.IntegrationSW;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class IntegrationLayerReader extends ExcelReader {
	static final Logger loger = Logger.getLogger(IntegrationLayerReader.class);

	private final String INTEGRATION_LAYER_FILE_NAME = "USCIS Architectural Components - Integration layer.xls";
	private final String NONPROD_INTEGRATION_LAYER_FILE_NAME = "USCIS Architectural Components - Non-Prod Integration layer.xls";
	private String NONPROD_SW_SHEET_NAME = "Unique NonProd SW Packages";
	private String SW_SHEET_NAME = "Unique Software Packages";
	private String COMP_SHEET_NAME = "Unique Integration Component";
	
	private int NAME_COL_NUM = 0;
	private int SW_ST_COL_NUM = 1;
	private int SW_DESC_COL_NUM = 2;
	private int SW_DISP_COL_NUM = 3;
	private int SW_SWV_COL_NUM = 4;
	private int SW_OS_COL_NUM = 5;
	private int SW_VER_COL_NUM = 6;
	private int SW_REL_COL_NUM = 7;
	private int SW_DA_COL_NUM = 8;

	private int COMP_IPT_COL_NUM = 1;
	private int COMP_ST_COL_NUM = 2;
	private int COMP_SUP_ST_COL_NUM = 3;
	private int COMP_DEF_COL_NUM = 4;
	private int COMP_DA_COL_NUM = 5;
	private int COMP_ISW_COL_NUM = 6;
	private int COMP_BSW_COL_NUM = 7;
	private int COMP_REL_COL_NUM = 8;
	private int COMP_SCONF_COL_NUM = 9;
	private int COMP_USE_SCONF_COL_NUM = 10;

	private int SW_MAX_ROWS = 46;
	private int NONPROD_SW_MAX_ROWS = 75;
	private int COMP_MAX_ROWS = 62;

	public SOAData extractAll(SOAData soaData) {
		extractProdSoftwarePkgs(soaData);
		extractNonProdSoftwarePkgs(soaData);
		extractProdIntegrationComp(soaData);
		return soaData;
	}
	
	private SOAData extractProdSoftwarePkgs(SOAData soaData) {
		loger.debug("Extracting Integration SW layer Sheet...");
		HSSFWorkbook w = openFile(INTEGRATION_LAYER_FILE_NAME);
		HSSFSheet s1 = w.getSheet(SW_SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);
		
		ArrayList<IntegrationSW> intSWList = new ArrayList<IntegrationSW>();
		soaData.intSWList = intSWList;
		
		int n = 2;
		while(true) {
			HSSFRow row = s1.getRow(n++);
			if(row == null)
				continue;
			String name = getCellStringValue(w, row.getCell(NAME_COL_NUM));;
			if(Utils.isEmpty(name)) {
				loger.warn("Integration SW Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			if("END".equalsIgnoreCase(name)) {
				break;
			}
			IntegrationSW iObj = new IntegrationSW();
			iObj.id = Utils.getNextId();
			iObj.name = name.trim();
			iObj.supported = getCellStringValue(w, row.getCell(SW_ST_COL_NUM));

			iObj.descTxt = getCellStringValue(w, row.getCell(SW_DESC_COL_NUM));
			iObj.disposition = getCellStringValue(w, row.getCell(SW_DISP_COL_NUM));
			iObj.swVendor = getCellStringValue(w, row.getCell(SW_SWV_COL_NUM));
			iObj.os = getCellStringValue(w, row.getCell(SW_OS_COL_NUM));
			iObj.version = getCellStringValue(w, row.getCell(SW_VER_COL_NUM));
			iObj.release = getCellStringValue(w, row.getCell(SW_REL_COL_NUM));
			iObj.designAspects = getCellStringValue(w, row.getCell(SW_DA_COL_NUM));
			//added on 07/19/2010 - RSingh
			iObj.envType = IntegrationSW.PROD_SW_TYPE;
			
			iObj.createDescription();
			iObj.prodConfig = true;
			
			if(Utils.findIntegrationSWByName(iObj.name, intSWList) == null)
				intSWList.add(iObj);
			else
				loger.error("Duplicate Integration SW [" + iObj.name + "] with same name found earlier at row : " + n + ", ignoring this to load.");
		}
		closeAll(w);
		return soaData;
	}
	
	private SOAData extractNonProdSoftwarePkgs(SOAData soaData) {
		loger.debug("Extracting Integration NONPROD SW layer Sheet...");
		HSSFWorkbook w = openFile(NONPROD_INTEGRATION_LAYER_FILE_NAME);
		HSSFSheet s1 = w.getSheet(NONPROD_SW_SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);
		
		List<IntegrationSW> intSWList = soaData.intSWList;

		for (int n = 2; n < NONPROD_SW_MAX_ROWS; n++) {
			HSSFRow row = s1.getRow(n);
			if(row == null)
				continue;
			String name = getCellStringValue(w, row.getCell(NAME_COL_NUM));;
			if(Utils.isEmpty(name)) {
				loger.warn("Integration Non-Prod SW Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			IntegrationSW iObj = new IntegrationSW();
			iObj.id = Utils.getNextId();
			iObj.name = name.trim();
			iObj.supported = getCellStringValue(w, row.getCell(SW_ST_COL_NUM));

			iObj.descTxt = getCellStringValue(w, row.getCell(SW_DESC_COL_NUM));
			iObj.disposition = getCellStringValue(w, row.getCell(SW_DISP_COL_NUM));
			iObj.swVendor = getCellStringValue(w, row.getCell(SW_SWV_COL_NUM));
			iObj.os = getCellStringValue(w, row.getCell(SW_OS_COL_NUM));
			iObj.version = getCellStringValue(w, row.getCell(SW_VER_COL_NUM));
			iObj.release = getCellStringValue(w, row.getCell(SW_REL_COL_NUM));
			iObj.designAspects = getCellStringValue(w, row.getCell(SW_DA_COL_NUM));
			//added on 07/19/2010 - RSingh
			iObj.envType = IntegrationSW.NONPROD_SW_TYPE;

			iObj.createDescription();
			iObj.prodConfig = false;
			
			if(Utils.findIntegrationSWByName(iObj.name, intSWList) == null)
				intSWList.add(iObj);
			else
				loger.error("Duplicate Non-Prod Integration SW [" + iObj.name + "] with same name found earlier at row : " + n + ", ignoring this to load.");
		}
		closeAll(w);
		return soaData;
	}

	private SOAData extractProdIntegrationComp(SOAData soaData) {
		loger.debug("Extracting Integration Component layer Sheet...");
		HSSFWorkbook w = openFile(INTEGRATION_LAYER_FILE_NAME);
		HSSFSheet s1 = w.getSheet(COMP_SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);
		
		ArrayList<IntegrationComp> intCompList = new ArrayList<IntegrationComp>();
		soaData.intCompList = intCompList;
		
		int n = 2;
		while(true) {
			HSSFRow row = s1.getRow(n++);
			if(row == null)
				continue;
			String name = getCellStringValue(w, row.getCell(NAME_COL_NUM));;
			if(Utils.isEmpty(name)) {
				loger.warn("Integration Comp Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			if("END".equalsIgnoreCase(name)) {
				break;
			}
			IntegrationComp iObj = new IntegrationComp();
			iObj.id = Utils.getNextId();
			iObj.name = name;
			iObj.wipt = getCellStringValue(w, row.getCell(COMP_IPT_COL_NUM));
			iObj.stereoType = getCellStringValue(w, row.getCell(COMP_ST_COL_NUM));
			iObj.supported = getCellStringValue(w, row.getCell(COMP_SUP_ST_COL_NUM));
			iObj.defination = getCellStringValue(w, row.getCell(COMP_DEF_COL_NUM));
			iObj.designAspects = getCellStringValue(w, row.getCell(COMP_DA_COL_NUM));
			iObj.intSWPkg = getCellStringValue(w, row.getCell(COMP_ISW_COL_NUM));
			iObj.buildSW = getCellStringValue(w, row.getCell(COMP_BSW_COL_NUM));
			iObj.release = getCellStringValue(w, row.getCell(COMP_REL_COL_NUM));
			iObj.security = getCellStringValue(w, row.getCell(COMP_SCONF_COL_NUM));
			iObj.securityConfig = getCellStringValue(w, row.getCell(COMP_USE_SCONF_COL_NUM));
			
			iObj.createDescription();
			if(Utils.findIntegrationUCByName(iObj.stereoType, iObj.name, intCompList) == null)
				intCompList.add(iObj);
			else
				loger.error("Duplicate Integration Comp [" + iObj.name + "] with same name found earlier at row : " + n + ", ignoring this to load.");
		}
		closeAll(w);
		return soaData;
	}

}
